package com.fram.service.api.v1.android.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.admin.android.withdrawal.request.WithdrawalAndroidRequest;
import com.fram.bean.admin.android.withdrawal.response.WithdrawalAndroidResponse;
import com.fram.bean.api.android.wallet.request.WithdrawalAddRequest;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.canstant.PublicConstant;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.encryption.AESUtils;
import com.fram.entity.UserTransactionFlow;
import com.fram.entity.UserVirtualWallet;
import com.fram.mapper.UserTransactionFlowMapper;
import com.fram.mapper.UserVirtualWalletMapper;
import com.fram.mapper.UserWithdrawCashListMapper;
import com.fram.service.api.v1.android.WithdrawalAndroidService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class WithdrawalAndroidServiceImpl implements WithdrawalAndroidService {

	@Autowired
	private UserWithdrawCashListMapper userWithdrawCashListMapper;
	@Autowired
	private UserVirtualWalletMapper userVirtualWalletMapper;
	@Autowired
	private UserTransactionFlowMapper userTransactionFlowMapper;
	
	private static String key = "PAYPWD_AESUTISSS";
	@Override
	public void add(WithdrawalAddRequest request) throws ProException {
		// 钱包
		UserVirtualWallet wallet = userVirtualWalletMapper.selectByUserId(request.getUserId());
		try {
			if (!PublicMethod.getMd5(AESUtils.encrypt(request.getPaypwd(), key), 1, true).equals(wallet.getPaypwd())) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "请输入正确密码");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(""+wallet.getBanlanceMoney());
		if (wallet.getBanlanceMoney().compareTo(request.getWithdrawFee()) == -1) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "提现金额超出钱包余额");
		}
		wallet.setBanlanceMoney(wallet.getBanlanceMoney().subtract(request.getWithdrawFee()));
		wallet.setSign(PublicMethod
				.getMd5(wallet.getUserId() + "" + wallet.getBanlanceMoney() + "" + wallet.getFrozenMoney(), 1, true));
		if (userVirtualWalletMapper.updateByPrimaryKeySelective(wallet) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		request.setCodeNumber(PublicMethod.getOrderNumber(PublicConstant.WITHDRAWAL_TYPE_1, null));
		request.setWithdrawStatus(1);//发起申请
		if(userWithdrawCashListMapper.insertSelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
//		// 流水信息 添加
//		UserTransactionFlow flow = new UserTransactionFlow();
//		flow.setUserId(request.getUserId());
//		flow.setResType(10);
//		flow.setFlowType(2);
//		flow.setPrice(request.getWithdrawFee());
//		flow.setReason("提现记录");
//		if (userTransactionFlowMapper.insertSelective(flow) <= 0) {
//			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
//		}
	}
	
	@Override
	public PageInfo<WithdrawalAndroidResponse> list(PageBean<WithdrawalAndroidRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<WithdrawalAndroidResponse> list = userWithdrawCashListMapper.listForAndroid(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public PageInfo<UserTransactionFlow> moneyDetailList(PageBean<UserTransactionFlow> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<UserTransactionFlow> list = userTransactionFlowMapper.listForAndroid(request.getParam());
		if(list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}
}
